package com.example.e;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.e.Adapter.SubmitProductAdapter;
import com.example.e.Bean.GoodsBean;
import com.example.e.Bean.Order;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class SubmitActivity extends AppCompatActivity {

    private static List<Order>history_order_data = new ArrayList<>();
    private String file_name3 = "order_history_data";

    private static List<GoodsBean> submit_list=new ArrayList<>();
    private RecyclerView recyclerviewSubmit;
    private LinearLayoutManager linearLayoutManager;
    private SubmitProductAdapter adapter;
    private TextView tv_totle_money,tv_zhifu;
    Double totleMoney=0.0,price;
    Integer num,totalcount=0;
    String str;
    private static DecimalFormat df;
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit);

        Intent intent = super.getIntent();
        submit_list = (List<GoodsBean>) intent.getSerializableExtra("submit_list");
        //intent.getDoubleExtra("totleMoney",totleMoney);
        tv_totle_money = (TextView) findViewById(R.id.tv_totle_money);
        df = new DecimalFormat("0.00");
       // tv_totle_money.setText("￥"+String.valueOf(df.format(totleMoney)));

        //Toast.makeText(context,"${totleMoney}",Toast.LENGTH_LONG).show();
        recyclerviewSubmit = (RecyclerView) findViewById(R.id.recyclerviewSubmit);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerviewSubmit.setLayoutManager(linearLayoutManager);
        adapter = new SubmitProductAdapter(submit_list);
        recyclerviewSubmit.setAdapter(adapter);

        for(int i = 0; i < submit_list.size(); i++){
            num = submit_list.get(i).getNum();
            str = submit_list.get(i).getPrice();
            price = Double.parseDouble(str);
            totleMoney += num*price;
        }
        for (int i = 0; i < submit_list.size(); i++){
            totalcount += submit_list.get(i).getNum();
        }
        tv_totle_money.setText("￥"+String.valueOf(df.format(totleMoney)));

        tv_zhifu = (TextView) findViewById(R.id.tv_zhifu);
        tv_zhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(SubmitActivity.this, MainActivity.class);
                //Bundle bundle = new Bundle();
                //bundle.putSerializable("submit_list", (Serializable) submit_list);
                //bundle.putInt("totalcount",totalcount);
                //bundle.putDouble("totleMoney",totleMoney);
                //bundle.putDouble("totleMoney",totleMoney);
                //intent.putExtras(bundle);
                //Toast.makeText(FoodActivity.this,totleMoney.toString(),Toast.LENGTH_LONG).show();
                Toast.makeText(SubmitActivity.this,"支付成功",Toast.LENGTH_LONG).show();
                startActivity(intent);
                save();
            }
        });
    }
    public void save(){
        Integer total_count =  totalcount;
        Double totle_Money = totleMoney;

        FileOutputStream out = null;
        BufferedWriter writer = null;
        try {
            out = openFileOutput(file_name3, Context.MODE_APPEND);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(total_count + "," + totle_Money);
            writer.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}